package cc.javaee.bbs.vo;

import cc.javaee.bbs.model.Bankuai;
import cc.javaee.bbs.model.Huifu;
import cc.javaee.bbs.model.Tiezi;
import cc.javaee.bbs.model.activity.BankuaiCustomer;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.var;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xdw
 */
public class TopicVO {

    @Getter
    @Setter
    private Integer id;
    @Getter
    @Setter
    private String custName;        //用户名
    @Getter
    @Setter
    private String custImg;         //用户图片
    @Getter
    @Setter
    private String context;         //内容
    @Getter
    @Setter
    private String fileImg;         //文件
    @Getter
    @Setter
    private Integer likeCount;      //点赞次数
    @Getter
    @Setter
    private Integer commentCount;   //评论次数
    @Getter
    @Setter
    private List<Comment> commentList;  //评论
    @Getter
    @Setter
    private Date createTime;
    @Getter
    @Setter
    private Integer myVoteStatus;
    @Getter
    @Setter
    private Integer myLikeStatus;

    @Setter
    private BankuaiCustomer bankuaiCustomer;
    @Setter
    private Map<Integer,Huifu> vote;

    @Data
    public static class Comment {
        private Integer id;
        private String custName;
        private String custImg;
        private String context;

        private String fromCustName;
        private String fromCustImg;

        private Date createTime;

        private List<String> imgList;

        private Integer myStatus = 0;

        private Integer likeCount = 0;
    }

    public static TopicVO conversionTopicVO(Tiezi tiezi, List<Huifu> huifus) {

        Map<Integer, Huifu> huifuMap = huifus.stream().collect(Collectors.toMap(Huifu::getId, e -> e));

        TopicVO topic = new TopicVO();
        topic.setId(tiezi.getId());
        topic.setCustName(tiezi.getTieziusername());
        topic.setCustImg(tiezi.getTieziuserimg());
        topic.setFileImg(tiezi.getFileImg());
        topic.setContext(tiezi.getContenthtml());
        topic.setMyVoteStatus(tiezi.getMyVoteStatus());
        topic.setMyLikeStatus(tiezi.getMyLikeStatus());
        int likeCount = 0;
        int commentCount = 0;
        List commentList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(huifus)) {
            Map<Integer, List<Huifu>> maphuifus = huifus.stream().collect(Collectors.groupingBy(Huifu::getType));//1回复 2上传文件 3投票 4点赞
            // 获取点赞
            List<Huifu> likes = maphuifus.get(4);
            if (likes != null) {
                topic.setLikeCount(likes.size());
                likeCount = likes.size();
            } else {
                topic.setLikeCount(0);
            }


            // 获取评论
            List<Huifu> comments = maphuifus.get(1);
            if (CollectionUtils.isNotEmpty(comments)) {
                commentCount = comments.size();
                if (CollectionUtils.isNotEmpty(comments)) {
                    comments.forEach((h) -> {
                        Comment comment = new Comment();
                        comment.setId(h.getId());
                        comment.setCustImg(h.getUserImg());
                        comment.setCustName(h.getName());
                        comment.setContext(h.getContenttxt());
                        comment.setCreateTime(h.getCreatetime());
                        if (h.getImg() != null) {
                            String img = h.getImg();
                            String[] split = img.split(",");
                            comment.setImgList(Arrays.asList(split));
                        }

                        if (h.getHuifuid() != null) {
                            Huifu huifu = huifuMap.get(h.getHuifuid());
                            if (huifu != null) {
                                comment.setFromCustImg(huifu.getUserImg());
                                comment.setFromCustName(huifu.getName());
                            }
                        }
                        commentList.add(comment);
                    });
                }
            }
        }
        topic.setCreateTime(tiezi.getCreatetime());
        topic.setLikeCount(likeCount);
        topic.setCommentCount(commentCount);
        topic.setCommentList(commentList);
        return topic;
    }

//    private static int[] type = {Calendar.MINUTE, Calendar.HOUR_OF_DAY, Calendar.DATE, Calendar.YEAR};
//
//    public static TopicVO conversionTopicVO(Tiezi tiezi, String archivalNumber) {
//        TopicVO topic = new TopicVO();
//        topic.setCustName("李冬冬");
//        topic.setCustImg("/upload/touxiang.jpg");
//        topic.setContext("内容");
//        topic.setFileImg("/upload/touxiang.jpg");
//        topic.setLikeCount(1000);
//        topic.setCommentCount(10000);
//        List<Comment> commentList = new ArrayList<>();
//
//        commentList.add(new Comment() {{
//            this.setCustName("李冬冬");
//            this.setContext("评论1");
//            this.setCustImg("/upload/touxiang.jpg");
//        }});
//        commentList.add(new Comment() {{
//            this.setCustName("李冬冬2");
//            this.setContext("评论2");
//            this.setCustImg("/upload/touxiang.jpg");
//        }});
//
//        topic.setCommentList(commentList);
//
//        Calendar cal = Calendar.getInstance();
////        cal.add(Calendar.DATE, -2);
////        cal.add(Calendar.HOUR_OF_DAY, -3);
//        Random random = new Random();
//
//        cal.add(type[random.nextInt(4)], 0 - (random.nextInt(8) + 2));
//
//        topic.setCreateTime(cal.getTime());
//
//        System.out.println(" 时间间隔： " + (System.currentTimeMillis() - cal.getTimeInMillis()));
//        System.out.println(" 日期： " + millisecondToDate(System.currentTimeMillis() - cal.getTimeInMillis()));
//
//        return topic;
//    }
//
//    private static String millisecondToDate(long ms) {
////        var time = parseFloat(msd) / 1000;
//        String time = "";
//        int second = (int) (ms / 1000);
//        String secondStr = (second % 60) + "秒";
//        time = secondStr + time;
//        int minutes = (int) (second / 60);
//        if (minutes > 0) {
//            String minutesStr = minutes % 60 + "分";
//            time = minutesStr + time;
//            int hour = (int) (minutes / 60);
//            if (hour > 0) {
//                String hourStr = hour % 24 + "小时";
//                time = hourStr + time;
//                int day = (int) (hour / 24);
//                if (day > 0) {
//                    time = day + "天" + time;
//                }
//            }
//        }
//        return time;
//    }
//
//    public static void main(String[] args) {
//        Calendar cal = Calendar.getInstance();
//        Random random = new Random();
//
//        cal.add(type[random.nextInt(4)], -random.nextInt(10));
//
//        System.out.println(millisecondToDate(System.currentTimeMillis() - cal.getTimeInMillis()));
//    }
}
